﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Validater;
using System.Drawing;
using COFE.Core.Utils;
using System.IO;
using System.Drawing.Imaging;
using COFE.Core.Entry;

namespace COFE.Core
{
    /// <summary>
    /// {90-100} Getting Icon and Thumbnail
    /// </summary>
    public enum IconProperties : int
    {
        [PropertyType(typeof(IResourceList))]
        [ResourceList(IconProperties.SmallIcon, IconProperties.LargeIcon, IconProperties.ExtraLargeIcon, IconProperties.JumboIcon)]
        [Resource(MediaType.ResourceList, true)]
        [Alias("Icon")]
        Icon = 90,

        /// <summary>
        /// Return an resource list that contains all supported thumbnail.
        /// e.g. Small.png, 
        /// </summary>
        [PropertyType(typeof(IResourceList))]
        [ResourceList(IconProperties.SmallThumbnail, IconProperties.MediumThumbnail, IconProperties.LargeThumbnail)]
        [Resource(MediaType.ResourceList, true)]
        [Alias("Thumbnail")]
        Thumbnail,

        /// <summary>
        /// Gets the Small Icon (16x16)
        /// </summary>
        [PropertyType(typeof(Bitmap))] 
        [BitmapSize(16, 16)]
        [Resource(MediaType.Png, false)]
        [Alias("SmallIcon")]
        SmallIcon,

        /// <summary>
        /// Gets the Large Icon (64x64)
        /// </summary>
        [PropertyType(typeof(Bitmap))]
        [BitmapSize(64, 64)]
        [Resource(MediaType.Png, false)]
        [Alias("LargeIcon")]
        LargeIcon,

        /// Gets the Extra Large Icon (128x128)
        /// </summary>
        [PropertyType(typeof(Bitmap))]
        [BitmapSize(128, 128)]
        [Resource(MediaType.Png, false)]
        [Alias("ExtraLargeIcon")]
        ExtraLargeIcon,

        /// <summary>
        /// Gets the Jumbo Icon (256x256)
        /// </summary>
        [PropertyType(typeof(Bitmap))]
        [BitmapSize(256, 256)]
        [Resource(MediaType.Png, false)]
        [Alias("JumboIcon")]
        JumboIcon,

        /// <summary>
        /// Get thumbnail of a image. (128x96).
        /// </summary>
        [PropertyType(typeof(Bitmap))]
        [BitmapSize(128, 96)]
        [Resource(MediaType.Png, false)]
        [Alias("SmallThumbnail")]
        SmallThumbnail,

        /// <summary>
        /// Get thumbnail of a image. (256x192).
        /// </summary>
        [PropertyType(typeof(Bitmap))]
        [BitmapSize(256, 192)]
        [Resource(MediaType.Png, false)]
        [Alias("MediumThumbnail")]
        MediumThumbnail,

        /// <summary>
        /// Get thumbnail of a image. (512x384).
        /// </summary>
        [PropertyType(typeof(Bitmap))]
        [BitmapSize(512, 384)]
        [Resource(MediaType.Png, false)]
        [Alias("LargeThumbnail")]
        LargeThumbnail    
    }

    public static class IconPropertiesExtension
    {
        public static Size GetDefaultSize(this IconProperties property)
        {
            foreach (IPropertyValidater propValidater in IPropertyValidaterExtension.GetPropertyValidaters(property))
                if (propValidater is BitmapSizeAttribute)
                {
                    BitmapSizeAttribute bitmapSizeAttribute = propValidater as BitmapSizeAttribute;
                    return new Size(bitmapSizeAttribute.Width, bitmapSizeAttribute.Height);
                }
            throw new ArgumentException("BitmapSizeAttribute not found.");
        }

        public static Bitmap EnsureBitmapSize(this Bitmap input, IconProperties property)
        {
            return BitmapUtils.EnsureBitmapSize(input, property.GetDefaultSize());
        }

        public static byte[] ConvertToBitmapArray(this Bitmap input, ImageFormat fmt)
        {
            MemoryStream ms = new MemoryStream();
            input.Save(ms, fmt);
            ms.Seek(0, SeekOrigin.Begin);
            return ms.ToArray();
        }

        public static byte[] ConvertToBitmapArray(this Bitmap input)
        {
            return input.ConvertToBitmapArray(ImageFormat.Png);
        }
        
    }
}
